'''
Created on Jun 2, 2009

@author: mkiyer
'''

from optparse import OptionParser
from veggie.parser.hpeakparser import HPeakAnnotation
from veggie.parser.bedparser import BEDParser, BEDAnnotation
import sys

def convert_hpeak_2_bed(hpeak_fhd, outfhd=sys.stdout):
    # read input file
    for i, hpeakline in enumerate(HPeakAnnotation.parseFile(hpeak_fhd)):
        bedline = BEDAnnotation()
        bedline.chrom = hpeakline.chrom
        bedline.chromStart = hpeakline.chromStart
        bedline.chromEnd = hpeakline.chromEnd
        bedline.name = 'HPEAK_peak_' + str(i)
        bedline.score = round(hpeakline.peakHeight, 2)
        print >>outfhd, bedline

if __name__ == '__main__':
    # setup command line parser
    usage = "usage: %prog [options] infile outfile"
    
    # parse command line
    optparser = OptionParser()
    (options, args) = optparser.parse_args()
    # check options, args
    if len(args) < 1:
        optparser.error('No input file specified')
    # setup input and output files
    infhd = open(args[0])
    if len(args) < 2:
        outfhd = sys.stdout
    else:
        outfhd = open(args[1], 'w')

    # convert
    convert_hpeak_2_bed(infhd, outfhd)